/**
 * 请求本地文件系统对象
 * @param {Object} path 本地文件系统常量
 */
export function requestFileSystem(type) {
	return new Promise((resolve, reject) => {
		plus.io.requestFileSystem(type, resolve, reject)
	})
}

/**
 * 创建或打开文件
 * @param {Object} directoryEntry
 * @param {Object} path 要操作文件相对于当前目录的地址
 * @param {Object} flag 要操作文件或目录的参数
 */
export function getFile(directoryEntry, path, flag) {
	return new Promise((resolve, reject) => {
		directoryEntry.getFile(path, flag, resolve, reject)
	})
}


/**
 * 创建或打开子目录
 * @param {Object} directoryEntry
 * @param {Object} path 要操作目录相对于当前目录的地址
 * @param {Object} flag 要操作文件或目录的参数
 */
export function getDirectory(directoryEntry, path, flag) {
	return new Promise((resolve, reject) => {
		directoryEntry.getDirectory(path, flag, resolve, reject)
	})
}

/**
 * 获取文件关联的写文件操作对象FileWriter
 * @param {Object} fileEntr
 * @param {Object} path 要操作文件相对于当前目录的地址
 * @param {Object} flag 要操作文件或目录的参数
 */
export function createWriter(fileEntry) {
	return new Promise((resolve, reject) => {
		fileEntry.createWriter(resolve, reject)
	})
}

/**
 * 创建一个目录读取对象，用户读取目下的文件及子目录。
 * @param {Object} FileWriter
 * @param {Object} path 要操作文件相对于当前目录的地址
 * @param {Object} flag 要操作文件或目录的参数
 */
export function createReader(directoryEntry) {
	return new Promise((resolve, reject) => {
		fileEntry.createReader(resolve, reject)
	})
}

/**
 * 通过一个fileWriter来写入文本， 写入数据会覆盖原来的数据
 * @param {Object} writer
 * @param {Object} strData 需要写入的数据，只能写入文字
 */
export function writerStrToFile(fileWriter, strData) {
	return new Promise((resolve, reject) => {
		fileWriter.onwrite = (e) => resolve(e)
		fileWriter.onerror = (e) => reject(e)
		fileWriter.write(strData)
	})
}

/**
 * android或Ios中将Base64字符串写入到一个文件
 * 回调参数为fullPath
 * @param {Object} fullPath
 * @param {Object} base64Str
 */
export function androidOrIosBase64ToFile(fullPath, base64Str) {
	return new Promise((resolve, reject) => {
		let platform = uni.getSystemInfoSync().platform
		if (platform == 'android') {
			var Base64 = plus.android.importClass("android.util.Base64");
			var FileOutputStream = plus.android.importClass("java.io.FileOutputStream");
			try {
				var out = new FileOutputStream(fullPath);
				// 此处Base64.decode有长度限制，如果不能满足需求，可以考虑换成官方原生插件市场的【Base64转文件】
				var bytes = Base64.decode(base64Str, Base64.DEFAULT);
				out.write(bytes);
				out.close();
				resolve(fullPath) //写入成功
			} catch (e) {
				reject(e)
			}
		} else if (platform == 'ios') {
			var NSData = plus.ios.importClass('NSData');
			var nsData = new NSData();
			nsData = nsData.initWithBase64EncodedStringoptions(base64Str, 0);
			if (nsData) {
				nsData.plusCallMethod({
					writeToFile: fullPath,
					atomically: true
				});
				plus.ios.deleteObject(nsData); //写入成功后删除缓存
			}
			resolve(fullPath) //写入成功
		}
	})
}
/**
 * 递归创建文件夹
 * @param {Object} directoryEntry
 * @param {Object} dirPath  可以是多级目录
 */
export async function recursionGetDirectory(directoryEntry, dirPath) {
	let dirNameArr = dirPath.split("/")
	let flag = {
		create: true
	}
	let temDdirEntry = directoryEntry
	for (let dir of dirNameArr) {
		temDdirEntry = await this.getDirectory(temDdirEntry, dir, flag)
	}
	return temDdirEntry;
}
/**
 * 递归创建文件夹
 * @param {Object} directoryEntry
 * @param {Object} path  可以是多级目录，使用/分割目录，最后一个当做文件名称
 */
export async function recursionGetFile(directoryEntry, path) {
	let flag = {
		create: true
	}
	let index = path.lastIndexOf("/")
	let dirPath = path.substring(0, index)
	let fileName = path.substring(index + 1)
	let lastDirEntry = null
	if (index > 0) { //表示有目录
		lastDirEntry = await this.recursionGetDirectory(directoryEntry, dirPath)
	} else {
		lastDirEntry = directoryEntry
	}
	let fileEntry = await this.getFile(lastDirEntry, fileName, flag)
	return fileEntry;
}